package com.hksj.am.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.am.mapper.AmmoniaCockpitMapper;
import com.hksj.am.query.AlarmDetailQuery;
import com.hksj.am.vo.AlarmDetailExcelVO;
import com.hksj.am.vo.AlarmDeviceInfoVO;
import com.hksj.cbm.entity.SpecialityModel;
import com.hksj.cbm.service.SpecialityModelService;
import com.hksj.common.core.common.Constants;
import com.hksj.common.core.common.SpecialityModelCodeEnum;
import com.hksj.common.core.util.DateUtil;
import com.hksj.common.core.util.EasyExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author fjt
 * @data 2024/11/6 18:20
 * @describe 氨气监测——报警列表
 */
@Service
public class AmmoniaAlarmService {


    @Autowired
    private AmmoniaCockpitMapper ammoniaCockpitMapper;
    @Autowired
    private SpecialityModelService specialityModelService;


    /**
     * 分页查询报警列表
     * @param query
     * @return
     */
    public Page<AlarmDeviceInfoVO> pageAlarm(AlarmDetailQuery query){
        query.setCode(SpecialityModelCodeEnum.ammonia_monitor.getCode());
        if (StringUtils.isNotBlank(query.getStartTimeStr())){
            query.setStartTime(DateUtil.getYmdHms(query.getStartTimeStr() + " 00:00:00"));
        }
        if (StringUtils.isNotBlank(query.getEndTimeStr())){
            query.setEndTime(DateUtil.getYmdHms(query.getEndTimeStr() + " 23:59:59"));
        }
        Page<AlarmDeviceInfoVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        List<AlarmDeviceInfoVO> list = ammoniaCockpitMapper.queryAlarmDetail(query, page);
        page.setRecords(list);
        return page;
    }


    /**
     * 报警记录导出
     * @param response
     * @param query
     */
    public void exportList(HttpServletResponse response, AlarmDetailQuery query){
        query.setCode(SpecialityModelCodeEnum.ammonia_monitor.getCode());
        if (StringUtils.isNotBlank(query.getStartTimeStr())){
            query.setStartTime(DateUtil.getYmdHms(query.getStartTimeStr() + " 00:00:00"));
        }
        if (StringUtils.isNotBlank(query.getEndTimeStr())){
            query.setEndTime(DateUtil.getYmdHms(query.getEndTimeStr() + " 23:59:59"));
        }
        List<AlarmDeviceInfoVO> list = ammoniaCockpitMapper.queryAlarmDetail(query, null);

        List<AlarmDetailExcelVO> dataList = new ArrayList<>();
        if (CollUtil.isNotEmpty(list)){
            LambdaQueryWrapper<SpecialityModel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(SpecialityModel::getCode, SpecialityModelCodeEnum.air_conditioning.getCode());
            SpecialityModel specialityModel = specialityModelService.getOne(queryWrapper);
            if (ObjectUtil.isNull(specialityModel)){
                throw new RuntimeException("氨气监测系统缺少专业模型配置信息");
            }
            // 统计方式(1=按楼层统计(此时没有科室) 2=按科室统计)
            Integer statisticalMethod = specialityModel.getStatisticalMethod();
            if (Constants.ONE.equals(statisticalMethod)){
                list.forEach(alarmDeviceInfoVO -> {
                    // 楼栋->楼层->房间
                    StringBuilder deviceLocation = new StringBuilder(alarmDeviceInfoVO.getBuildingName());
                    if (StringUtils.isNotBlank(alarmDeviceInfoVO.getFloorName())){
                        deviceLocation.append("->").append(alarmDeviceInfoVO.getFloorName());
                    }
                    if (StringUtils.isNotBlank(alarmDeviceInfoVO.getRoomName())){
                        deviceLocation.append("->").append(alarmDeviceInfoVO.getRoomName());
                    }
                    AlarmDetailExcelVO excelVO = AlarmDetailExcelVO.builder().deviceLocation(deviceLocation.toString())
                            .deviceName(alarmDeviceInfoVO.getDeviceName()).value(alarmDeviceInfoVO.getValue())
                            .createTime(alarmDeviceInfoVO.getCreateTime())
                            .build();
                    dataList.add(excelVO);
                });
            }

            // 2=按科室统计
            if (Constants.TWO.equals(statisticalMethod)){
                list.forEach(alarmDeviceInfoVO -> {
                    // 楼栋->楼层->科室->房间
                    StringBuilder deviceLocation = new StringBuilder(alarmDeviceInfoVO.getBuildingName());
                    if (StringUtils.isNotBlank(alarmDeviceInfoVO.getFloorName())){
                        deviceLocation.append("->").append(alarmDeviceInfoVO.getFloorName());
                    }
                    if (StringUtils.isNotBlank(alarmDeviceInfoVO.getDepartmentItemValueName())){
                        deviceLocation.append("->").append(alarmDeviceInfoVO.getDepartmentItemValueName());
                    }
                    if (StringUtils.isNotBlank(alarmDeviceInfoVO.getRoomName())){
                        deviceLocation.append("->").append(alarmDeviceInfoVO.getRoomName());
                    }
                    AlarmDetailExcelVO excelVO = AlarmDetailExcelVO.builder().deviceLocation(deviceLocation.toString())
                            .deviceName(alarmDeviceInfoVO.getDeviceName()).value(alarmDeviceInfoVO.getValue())
                            .createTime(alarmDeviceInfoVO.getCreateTime())
                            .build();
                    dataList.add(excelVO);
                });
            }
        }
        String fileName = new StringBuffer().append("氨气监测—报警记录").append(DateUtil.getYmdHms()).append(".xlsx").toString();
        EasyExcelUtil.exportExcelData(response, fileName, dataList, AlarmDetailExcelVO.class);
    }


}
